package org.grew.openEntity.controller;

import org.grew.openEntity.po.logic.ProjectObj;
import org.grew.openEntity.po.system.PageObj;
import org.grew.openEntity.service.ColumnService;
import org.grew.openEntity.service.EditionService;
import org.grew.openEntity.service.EntityService;
import org.grew.openEntity.service.ProjectService;
import org.grew.openEntity.tools.AlertCode;
import org.grew.openEntity.vo.EditionListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: open_entity
 * @author: todd
 * @create: 2019-01-06 18:30
 * @description: ${description}
 */
@Controller
public class IndexController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private EditionService editionService;

    @Autowired
    private EntityService entityService;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private AlertCode alertCode;


    @GetMapping(value = "/")
    public String toFirstPage() {
        return "redirect:/page";
    }

    @GetMapping(value = "page")
    public String toPage(Model model) {
        model.addAttribute("projectCount", projectService.getProjectCount());
        model.addAttribute("editionCount", editionService.getEditionCount());
        model.addAttribute("entityCount", entityService.getEntityCount());
        model.addAttribute("columnCount", columnService.getColumnCount());
        return "index";
    }


    @GetMapping(value = "{name}/createPage")
    public String createPage(@PathVariable("name") String name, @RequestParam(value = "code", required = false) Integer code, Model model) {
        if (code != null) {
            model.addAttribute("alertObj", alertCode.getMsgByCode(code));
        } else {
            model.addAttribute("alertObj", null);
        }
        return "create".concat(name = name.substring(0, 1).toUpperCase().concat(name.substring(1, name.length())));
    }

    @GetMapping(value = "{name}/listPage")
    public String listPage(@PathVariable("name") String name, @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,Model model) {
        Map<String,Object> result = new HashMap<String, Object>();
        switch (name) {
            case "project":
                Page<ProjectObj> page = projectService.getPageByProject(pageIndex,pageSize);
                result.put("objList",page.getContent());
                result.put("pageIndex",pageIndex);
                result.put("totalPage",page.getTotalPages());
                result.put("page",new PageObj(pageIndex,page.getTotalPages(),"/project/listPage?pageIndex="));
                break;
            case "edition":
                EditionListVO editionList = editionService.getEditionList(pageIndex, pageSize);
                result.put("objList",editionList.getList());
                result.put("pageIndex",pageIndex);
                result.put("totalPage",editionList.getTotalPages());
                result.put("page",new PageObj(pageIndex,editionList.getTotalPages(),"/edition/listPage?pageIndex="));
                break;
            case "entity":
                break;
            default:
                break;
        }
        model.addAllAttributes(result);
        return "list".concat(name = name.substring(0, 1).toUpperCase().concat(name.substring(1, name.length())));
    }

}
